using System;
using System.Collections.Generic;
using System.Text;

namespace OfficeClip.OpenSource.LogShipping
{
    public class Program
    {
        public static OpenSource.Utility.Log Log;
        static int Main(string[] args)
        {
            if ((args.Length % 2 != 0) || args.Length == 0)
            {
                Console.WriteLine("Invalid number of arguments");
                Console.WriteLine("Usage:");
                Console.WriteLine(
                 @"
     OfficeClip.OpenSource.LogShipping -c command 
     -d databasename
     -f foldername
     -s connection-string
     [-bt backup-time]
     [-bd old-backups]                 
     [-l log-folder-path] 
     [-lrem log-remove-days]
                
Everytime this program is run a full-backup or transaction log backup is taken or restored (see below for options)

This program can be scheduled to run at regular intervals using the Microsoft Scheduler

Note: OfficeClip database must be changed to Full Recovery mode (via Sql Management Studio) if transaction logs are created.

if the arguments have blank spaces please put them in double quotes """"

-d is the officeclip database name that is supposed to be backed up or restored

-f is the folder name where the backup will be stored and restored from, do not put a trailing slash!

-s is the sql connection string, the connection string should have admin capabilities (preferably for the Master database)

-bt is the machine time when the full-backup is taken every day. It should be specified in hour (integer 0-23)

if -bt set to -1 then full-backup will be taken every time the program is run and no transaction log will be taken

if -bt is not specified, full-backup will be done at 1:00AM machine time

-bd shows number of days the old backups needs to be preserved. If not specified, this value is fixed at 7

if -l is not specified, log files will be created in the log folder one level below

if -lrem is not specified, all log files older than 2 days will be removed

    Commands: (with -c option)

    backup: Backup the database and/or transaction logs.
    restore: Restore the database and transaction logs.

    Error Codes:

    -1:	Invalid number of arguments.
    -2:	Invalid command or command not supported.
    -9:	An unhandled exception occurred.
				");

				return -1;
			}
            else
            {
                string command = string.Empty;
                string dbName = string.Empty;
                string filePath = string.Empty;
                string logFolder = OpenSource.Utility.Constants.GetSetupDir() + @"\log";
                int logRemoveDays = 2;
                int oldBackupPreserveDays = 7;
                int backupTime = 1;
                string connection = string.Empty;
                for (int ii = 0; ii < args.Length; ii += 2)
                {
                    switch (args[ii])
                    {
                        case "-c":
                            command = args[ii + 1];
                            break;
                        case "-d":
                            dbName = args[ii + 1];
                            break;
                        case "-f":
                            filePath = args[ii + 1]; 
                            break;
                        case "-s":
                            connection = args[ii + 1];
                            break;
                        case "-bt":
                            backupTime = Convert.ToInt32(args[ii + 1]);
                            break;
                        case "-bd":
                            oldBackupPreserveDays = Convert.ToInt32(args[ii + 1]);
                            break;
                        case "-l":
                            logFolder = args[ii + 1];
                            break;
                        case "-lrem":
                            logRemoveDays = Convert.ToInt32(args[ii + 1]);
                            break;
                    }
                }
                if (
                    (command == string.Empty) || 
                    (dbName == string.Empty) || 
                    (filePath == string.Empty) ||
                    (connection == string.Empty) 
                    )
                {
                    Console.WriteLine("Error: All mandatory arguments should be specified");
                    return -9;
                }
                if (!System.IO.Directory.Exists(filePath))
                {
                    Console.WriteLine("Error: The folder name specified does not exist");
                    return -9;
                }
                Log = new OfficeClip.OpenSource.Utility.Log(logFolder, logRemoveDays);
                if (command.ToLower() == "backup")
                {
                    Backup.BackupDatabase(dbName, filePath, backupTime, connection, oldBackupPreserveDays);
                }
                else if (command.ToLower() == "restore")
                {
                    Restore.RestoreDatabase(dbName, filePath, connection);
                }
                else
                {
                    Console.WriteLine("Error: command should be either backup or restore");
                }
            }
            return 0;
        }
    }
}
